package study.stream;

import java.util.Arrays;
import java.util.function.LongSupplier;
import java.util.stream.LongStream;

public class StreamTest {
    public static void main(String[] args) {
        LongStream fib = LongStream.generate(new FibSupplier());
        // 打印Fibonacci数列：1，1，2，3，5，8，13，21...
//        fib.limit(10).forEach(System.out::println);

        long[] longs = fib.limit(20).toArray();
        System.out.println(Arrays.toString(longs));
    }
}

class FibSupplier implements LongSupplier {
    long pre = 1;
    long current = 1;

    @Override
    public long getAsLong() {
        long p = pre;
        long next = pre + current;
        pre = current;
        current = next;
        return p;
    }
}

